package com.mustafaiev.tair.cts.facade;

import java.util.ArrayList;
import java.util.List;

import org.dozer.Mapper;

import com.mustafaiev.tair.cts.dao.IBasketsBatchDAO;
import com.mustafaiev.tair.cts.exeptions.DataNotRemovedException;
import com.mustafaiev.tair.cts.exeptions.DataNotStoredException;
import com.mustafaiev.tair.cts.model.BasketsBatch;
import com.mustafaiev.tair.cts.model.BasketsBatchFilter;
import com.mustafaiev.tair.cts.tapestry.dto.BasketDTO;
import com.mustafaiev.tair.cts.tapestry.dto.BasketsBatchDTO;
import com.mustafaiev.tair.cts.tapestry.dto.BasketsBatchFilterDTO;
import com.mustafaiev.tair.cts.tapestry.dto.PurchaseDTO;

public class BasketsBatchFacade implements IBasketsBatchFacade {

	private final IBasketsBatchDAO basketsBatchDAO;

	private final Mapper mapper;

	public BasketsBatchFacade(final IBasketsBatchDAO basketsBatchDAO,
			final Mapper mapper) {
		this.basketsBatchDAO = basketsBatchDAO;
		this.mapper = mapper;
	}

	@Override
	public void save(final BasketsBatchDTO basketsBatch)
			throws DataNotStoredException {
		setBatchFields(basketsBatch);
		final BasketsBatch batch = this.mapper.map(basketsBatch,
				BasketsBatch.class);
		this.basketsBatchDAO.doSave(batch);
	}

	@Override
	public void delete(final BasketsBatchDTO basketsBatch)
			throws DataNotRemovedException {
		final BasketsBatch batch = this.mapper.map(basketsBatch,
				BasketsBatch.class);
		this.basketsBatchDAO.doDelete(batch);
	}

	@Override
	public List<BasketsBatchDTO> retreiveBatches() {
		final List<BasketsBatch> batches = this.basketsBatchDAO
				.retreiveBatches();
		final List<BasketsBatchDTO> batchesDTO = new ArrayList<BasketsBatchDTO>();
		if (batches != null) {
			for (final BasketsBatch basketsBatch : batches) {
				final BasketsBatchDTO dto = this.mapper.map(basketsBatch,
						BasketsBatchDTO.class);
				batchesDTO.add(dto);
			}
		}
		return batchesDTO;
	}

	@Override
	public BasketsBatchDTO retrieveBatch(final Long batchId) {
		final BasketsBatch basketsBatch = this.basketsBatchDAO
				.retrieveBatch(batchId);
		return this.mapper.map(basketsBatch, BasketsBatchDTO.class);
	}

	@Override
	public List<BasketsBatchDTO> retrieveBatchesByFilter(
			final BasketsBatchFilterDTO filter) {
		final BasketsBatchFilter batchFilter = this.mapper.map(filter,
				BasketsBatchFilter.class);
		final List<BasketsBatch> batches = this.basketsBatchDAO
				.retrieveBatchesByFilter(batchFilter);
		final List<BasketsBatchDTO> batchesDTO = new ArrayList<BasketsBatchDTO>();
		for (final BasketsBatch basketsBatch : batches) {
			final BasketsBatchDTO dto = this.mapper.map(basketsBatch,
					BasketsBatchDTO.class);
			batchesDTO.add(dto);
		}
		return batchesDTO;
	}

	private void setBatchFields(final BasketsBatchDTO basketsBatch) {
		final List<BasketDTO> basketsDTO = basketsBatch.getBaskets();
		double totalCosts = 0;
		for (final BasketDTO basketDTO : basketsDTO) {
			final List<PurchaseDTO> purchases = basketDTO.getPurchases();
			for (final PurchaseDTO purchaseDTO : purchases) {
				totalCosts = totalCosts + purchaseDTO.getCost();
			}
		}
		basketsBatch.setBasketsCount(basketsDTO.size());
		basketsBatch.setTotalCosts(totalCosts);
	}

}
